<template>
  <!-- 提醒配置 -->
  <view class="tipCongig">
    <scroller class="scroller" ref="scroller" resize="none">

      <div class="wrapper" v-if="data">

        <VSwitc v-if="isIos" class="switc" v-model="data.appNotification" :title="T('APP提醒')"
          @change="switcClick('app', $event)" />
        <div class="line line1"></div>

        <VSwitc class="switc" v-model="data.tgNotification" :title="T('绑定telegram')"
          @change="switcClick('tg', $event)" />
        <div class="line line1"></div>

        <div class="tg-ext">
          <div class="tg-ext-top">
            <text class="all font28 text3">{{T('telegram机器人')}}</text>
            <text class="font28 main-t1" @click="tgClick">{{T('使用教程')}}</text>
          </div>
          <div class='frame bg2' @click="orderTypeClick">
            <text class="all font28 text2">{{ data.tgRobotUserName }}</text>
            <LImage class="img-arrow-down" :src="Config.img('ic_ai_copy.png')" @click="copyRobot"></LImage>
          </div>
        </div>

        <div class="tg-ext">
          <div class="tg-ext-top">
            <text class="all font28 text3">{{T('身份唯一码')}}</text>
          </div>
          <div class='frame bg2' @click="orderTypeClick">
            <text class="all font28 text2">{{ data.tgRandomCode }}</text>
            <LImage class="img-arrow-down" :src="Config.img('ic_ai_copy.png')" @click="copyCode"></LImage>
          </div>
        </div>

        <div class="line line1 mb30"></div>

        <VSwitc class="switc" v-model="data.phNotification" :title="T('电话通知')" @change="switcClick('ph', $event)" />
        <div class="line line1"></div>

        <div class="switc row acenter jbetween">
          <div class="row acenter">
            <text class="font28 text3 width140">{{T('电话')}}</text>
            <input ref="input" class="input-content font30 text1" :placeholder="T('请输入电话号码')" v-model="data.phone" />
            <!-- <text class="font30 text1">{{ data.phone }}</text> -->
          </div>
          <div class="but" @click="httpTest">
            <text class="font26 main-t1">{{T('电话测试')}}</text>
          </div>
        </div>
        <div class="line line1"></div>

        <div class="switc row acenter jbetween">

          <div class="row acenter">
            <text class="font28 text3 mr20">{{T('剩余条数')}}</text>
            <text class="font30 text1">{{data.phCount}}</text>
          </div>
          <div class="but" @click="phoneBuy">
            <text class="font26 main-t1">{{T('购买')}}</text>
          </div>
        </div>

        <div class="line line1 mb30"></div>
        <text class="font30 main-t1">{{T('注意')}}</text>
        <text class="font24 text3 mt10">{{T('由于运营商限制，每24小时内最多提醒20条语音。')}}</text>
      </div>
    </scroller>

  </view>
</template>

<style src="@/_css/style.css" ></style>
<style scoped>
  .tipCongig {
    position: relative;
    flex: 1;
    display: flex;
    flex-direction: column;
  }
  .scroller {
    display: flex;
    flex-direction: column;
  }
  .refresh {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    width: 750px;
    height: 80px;
  }
  .wrapper {
    display: flex;
    flex-direction: column;
    padding: 0 30px;
  }
  .switc {
    height: 118px;
  }
  .line {
    height: 1px;
  }
  .but {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 150px;
    height: 55px;
    border-radius: 8px;
    border-width: 1px;
    border-color: #1677fe;
  }
  .tg-ext {
    display: flex;
    flex-direction: column;
  }
  .tg-ext-top {
    height: 88px;
    flex-direction: row;
    display: flex;
    align-items: center;
  }
  .frame {
    height: 80px;
    border-radius: 4px;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    padding: 20px;
  }
  .img-arrow-down {
    width: 28px;
    height: 28px;
    color: white;
    margin-left: 20px;
  }
  .input-content {
    flex: 1;
    width: 400px;
    height: 118px;
    placeholder-color: #6e7488;
  }
</style>

<script>
  import VSwitc from "@/_public/VSwitc.vue";

  import jconfig from "@/_js/config";
  import jmodal from "@/_js/modal";
  import jdebug from "@/_js/debug";
  import jhttps from "@/_js/https";
  import jnavigator from "@/_js/navigator";
  const clipboard = weex.requireModule("clipboard");
  import broadcast from "@/_js/broadcast";
  const animation = weex.requireModule("animation"); //动画
  const domModule = weex.requireModule("dom");
  const stream = weex.requireModule("stream"); //网络请求
  const weexFunction = weex.requireModule("WeexFunction");
  import I18N from "@/_i18n/i18n";
  var T = I18N.t;
  weexFunction.setTitle(T("提醒配置"));
  export default {
    components: {
      VSwitc,
    },

    data() {
      return {
        Config: jconfig,
        T: T,
        data: undefined,
        isExplain: false,
        isIos: false,
      };
    },

    created() {
      setTimeout(() => {
        // jnavigator.load("", "aide/tipConfig");
      }, 5000);
      this.isIos = weex.config.env.platform.toUpperCase() != "ANDROID";

      broadcast.subscribe("Vip", broadcast.PHONE_BUY_OK, () => {
        this.httpAiSet();
      });
    },
    mounted() {
      this.httpAiSet();
    },

    methods: {
      /********************************** HTTPS*/
      //https AI提醒配置
      httpAiSet() {
        var param = {};
        jhttps.post(
          "indicator/queryNotificationSettings",
          param,
          true,
          (data) => {
            if (data.status == 200) {
              this.data = data.data;
              jdebug.log(this.data);
            } else {
              jmodal.toast(data.msg);
            }
          }
        );
      },

      //https 获取Tgid
      httpGetTelegarmChatId(bol) {
        var param = { tgRandomCode: this.data.tgRandomCode };
        jhttps.postX("/indicator/getTelegarmChatId", param, true, (data) => {
          jmodal.hideLoad();
          if (data.status == 200) {
            this.httpOpenTg(bol);
          } else if (data.status == 17001) {
            jmodal.toast("ChatId 不存在，弹出教程");
            this.isExplain = true;
            setTimeout(() => {
              this.httpAiSet();
            }, 3000);
          } else {
            this.data.tgNotification = !bol;
            jmodal.toast(data.msg);
          }
        });
      },

      //https 开启APp提醒
      httpOpenApp(bol) {
        jmodal.showLoad();
        var param = { appNotification: bol };
        jhttps.postX("indicator/openAppNotification", param, true, (data) => {
          if (data.status == 200) {
            jmodal.toast(T("操作成功"));
          } else {
            this.data.appNotification = !bol;
            jmodal.toast(data.msg);
          }
        });
      },

      //https 开启tg提醒
      httpOpenTg(bol) {
        jmodal.showLoad();
        var param = { tgNotification: bol };
        jhttps.postX(
          "indicator/openTelegramNotification",
          param,
          true,
          (data) => {
            if (data.status == 200) {
              jmodal.hideLoad();
              jmodal.toast(T("操作成功"));
            } else {
              this.data.tgNotification = !bol;
              jmodal.toast(data.msg);
            }
          }
        );
      },

      //https 开启电话提醒
      httpOpenPh(bol) {
        jmodal.showLoad();
        var param = {
          areaCode: "86",
          phNotification: bol,
          phone: this.data.phone,
        };
        jhttps.postX("indicator/openPhoneNotification", param, true, (data) => {
          jmodal.hideLoad();
          if (data.status == 200) {
            jmodal.toast(T("操作成功"));
          } else {
            this.data.phNotification = !bol;
            jmodal.toast(data.msg);
          }
        });
      },
      //语音测试
      httpTest() {
        jmodal.showLoad();
        var param = { areaCode: "86", phone: this.data.phone };
        jhttps.post("indicator/checkOutVoice", param, true, (data) => {
          jmodal.hideLoad();
          if (data.status == 200) {
            jmodal.toast(T("操作成功"));
          } else {
            jmodal.toast(data.msg);
          }
        });
      },

      /********************************** 点击*/
      //开关改变
      switcClick(type, bol) {
        if (type == "app") {
          this.httpOpenApp(bol);
          return;
        }

        if (type == "tg") {
          if (bol) {
            jmodal.showLoad();
            this.httpGetTelegarmChatId(bol);
          } else {
            this.httpOpenTg(bol);
          }
          return;
        }

        if (type == "ph") {
          this.httpOpenPh(bol);
          return;
        }
      },

      copyRobot() {
        clipboard.setString(this.data.tgRobotUserName);
        jmodal.toast(T("复制成功"));
      },
      copyCode() {
        clipboard.setString(this.data.tgRandomCode);
        jmodal.toast(T("复制成功"));
      },
      //TG说明点击
      tgClick() {
        weexFunction.showWebUrl(this.data.tgGuideUrl);
      },
      phoneBuy() {
        jnavigator.openDialog("aide/PhoneBuy");
      },
    },
  };
</script>